﻿using System.Web;

namespace YanZhiwei.DotNet2.Utilities.AspNet
{
    /// <summary>
    /// NetWork 帮助类
    /// </summary>
    public class NetWorkHelper
    {
        #region 获得用户IP
        /// <summary>
        /// 获得用户IP
        /// </summary>
        public static string GetUserIp()
        {
            string _userIp;
            string[] _userIpTmp;
            bool _result = false;
            if (HttpContext.Current.Request.ServerVariables["HTTP_X_ForWARDED_For"] == null)
            {
                _userIp = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString();
            }
            else
            {
                _userIp = HttpContext.Current.Request.ServerVariables["HTTP_X_ForWARDED_For"].ToString();
            }

            if (_userIp.Length > 15)
            {
                _result = true;
            }
            else
            {
                _userIpTmp = _userIp.Split('.');
                if (_userIpTmp.Length == 4)
                {
                    for (int i = 0; i < _userIpTmp.Length; i++)
                    {
                        if (_userIpTmp[i].Length > 3)
                            _result = true;
                    }
                }
                else
                {
                    _result = true;
                }
            }

            if (_result)
                return "1.1.1.1";
            else
                return _userIp;
        }
        #endregion
    }
}
